package com.example.leaderapplication.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.leaderapplication.Bean.loginByLeaderResultBean;
import com.example.leaderapplication.R;
import com.example.leaderapplication.View.Activity.BanCi.BanCiSettingActivity;
import com.example.leaderapplication.View.Activity.CompreDisplayActivity;
import com.example.leaderapplication.View.Activity.ShipOrder.ShipOrderActivity;

import java.util.List;

public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ItemViewHolder> {
    private Context context;
    private List<loginByLeaderResultBean.DataBean.MenuListBean> date;


    public HomeRecyclerViewAdapter(Context context, List<loginByLeaderResultBean.DataBean.MenuListBean> date) {
        this.context = context;
        this.date = date;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.rv_tab_left, null);
        ItemViewHolder holder = new ItemViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        if (position % 2 == 0) {
            holder.itemLeft.setBackgroundResource(R.drawable.shape_tab_left);
        } else {
            holder.itemLeft.setBackgroundResource(R.drawable.shape_tab_right);
        }
        if (date.get(position) != null) {
            holder.nameLeft.setText(date.get(position).getMeta().getTitle());
            Glide.with(context)
                    .load(date.get(position).getMeta().getIcon())
                    .into(holder.imageLeft);

            holder.itemLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (date.get(position).getPath().trim().length() > 10) {
                        Intent intent = new Intent(context, CompreDisplayActivity.class);
                        intent.putExtra("Path", date.get(position).getPath());
                        context.startActivity(intent);
                    } else {
                        // 如果没有基础地址，那么就要更具具体情况去实现页面
                        if (date.get(position).getMeta().getTitle().equals("班次设置")){
                           context.startActivity(new Intent(context, BanCiSettingActivity.class));
                        }

                        // 由于没有游船订单 暂时用 快速查单 顶替
                        if (date.get(position).getMeta().getTitle().equals("快速查单")){
                            context.startActivity(new Intent(context, ShipOrderActivity.class));
                        }
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return date.size();
    }

    @Override
    public void unregisterAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
        if (observer != null) {
            super.unregisterAdapterDataObserver(observer);
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView nameLeft;
        private ImageView imageLeft;
        private LinearLayout itemLeft;


        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            int itemViewType = getItemViewType();

            nameLeft = itemView.findViewById(R.id.tv_tab_name);
            imageLeft = itemView.findViewById(R.id.iv_tab_image);
            itemLeft = itemView.findViewById(R.id.ll_tab_item);

        }
    }
}
